import sys
import os
import enum
import uuid
from PySide6.QtWidgets import *
from PySide6.QtGui import *
from PySide6.QtCore import *

class Demo(QWidget):
    class DataFlag(enum.Enum):
        ItemType=0
        ItemName=1

    def __init__(self, parent=None):
        super().__init__(parent)
        self.parent = parent
        layout = QVBoxLayout()
        layout.setAlignment(Qt.AlignmentFlag.AlignTop)

        toolBar=QToolBar()
        toolBar.addWidget(QLabel('ItemType:'))

        self.comboxItem=QComboBox()
        self.comboxItem.addItems(['SimpleText','Text','Line','Rect','Ellipse','Polygon','Path','Pixmap'])
        toolBar.addWidget(self.comboxItem)

        btn=QPushButton('Add')
        btn.clicked.connect(self.Add)
        toolBar.addWidget(btn)        

        btn=QPushButton('Remove')
        btn.clicked.connect(self.Remove)
        toolBar.addWidget(btn)        

        btn=QPushButton('Group')
        btn.clicked.connect(self.Group)
        toolBar.addWidget(btn)        

        btn=QPushButton('UnGroup')
        btn.clicked.connect(self.UnGroup)
        toolBar.addWidget(btn)

        layout.addWidget(toolBar)

        toolBar=QToolBar()
        toolBar.addWidget(QLabel('DragMode:'))
        cb=QComboBox()
        cb.addItems(QGraphicsView.DragMode.__members__.keys())
        cb.currentTextChanged.connect(self.OnDragModeChanged)
        toolBar.addWidget(cb)

        toolBar.addWidget(QLabel('CacheMode:'))
        cb=QComboBox()
        cb.addItems(QGraphicsView.CacheMode.__members__.keys())
        cb.currentTextChanged.connect(self.OnCacheModeChanged)
        toolBar.addWidget(cb)

        toolBar.addWidget(QLabel('Optimization:'))
        cb=QComboBox()
        cb.addItems(QGraphicsView.OptimizationFlag.__members__.keys())
        cb.currentTextChanged.connect(self.OnOptimizationChanged)
        toolBar.addWidget(cb)

        toolBar.addWidget(QLabel('ViewportUpdateMode:'))
        cb=QComboBox()
        cb.addItems(QGraphicsView.ViewportUpdateMode.__members__.keys())
        cb.currentTextChanged.connect(self.OnViewportUpdateModeChanged)
        toolBar.addWidget(cb)

        layout.addWidget(toolBar)

        self.scene=QGraphicsScene()
        self.scene.selectionChanged.connect(self.OnItemSelectionChanged)       
        self.view=QGraphicsView(self.scene)
        self.view.setRenderHints(QPainter.RenderHint.Antialiasing)#抗锯齿        
        layout.addWidget(self.view)

        self.setLayout(layout)
    def Add(self):
        itemType=self.comboxItem.currentText()
        if itemType is None:return
        match itemType:
            case 'SimpleText':
                item =QGraphicsSimpleTextItem()
                item.setData(Demo.DataFlag.ItemType.value,itemType)
                item.setData(Demo.DataFlag.ItemName.value,f'{itemType}_{uuid.uuid1().hex}')
                item.setFlags(QGraphicsItem.GraphicsItemFlag.ItemIsMovable|QGraphicsItem.GraphicsItemFlag.ItemIsSelectable)#可选择可移动
                item.setCacheMode(QGraphicsItem.CacheMode.ItemCoordinateCache)
                item.setText(itemType)
                self.scene.addItem(item)
            case 'Text':
                item =QGraphicsTextItem()
                item.setData(self.DataFlag.ItemType.value,itemType)
                item.setData(self.DataFlag.ItemName.value,f'{itemType}_{uuid.uuid1().hex}')
                item.setFlags(QGraphicsItem.GraphicsItemFlag.ItemIsMovable|QGraphicsItem.GraphicsItemFlag.ItemIsSelectable)#可选择可移动
                item.setCacheMode(QGraphicsItem.CacheMode.ItemCoordinateCache)
                item.setHtml("A rich text <br/> with hyperlinks <br/> <a href='www.baidu.com'>baidu</a>")
                self.scene.addItem(item)
            case "Line":#直线
                item =QGraphicsLineItem()
                item.setData(self.DataFlag.ItemType.value,itemType)
                item.setData(self.DataFlag.ItemName.value,f'{itemType}_{uuid.uuid1().hex}')
                item.setFlags(QGraphicsItem.GraphicsItemFlag.ItemIsMovable|QGraphicsItem.GraphicsItemFlag.ItemIsSelectable)#可选择可移动
                item.setCacheMode(QGraphicsItem.CacheMode.ItemCoordinateCache)
                item.setLine(100,50,300,50)
                self.scene.addItem(item)
            case "Rect":#正方形
                item =QGraphicsRectItem()
                item.setData(self.DataFlag.ItemType.value,itemType)
                item.setData(self.DataFlag.ItemName.value,f'{itemType}_{uuid.uuid1().hex}')
                item.setFlags(QGraphicsItem.GraphicsItemFlag.ItemIsMovable|QGraphicsItem.GraphicsItemFlag.ItemIsSelectable)#可选择可移动
                item.setCacheMode(QGraphicsItem.CacheMode.ItemCoordinateCache)
                item.setRect(100,60,100,50)
                self.scene.addItem(item)
            case "Ellipse":#椭圆
                item =QGraphicsEllipseItem()
                item.setData(self.DataFlag.ItemType.value,itemType)
                item.setData(self.DataFlag.ItemName.value,f'{itemType}_{uuid.uuid1().hex}')
                item.setFlags(QGraphicsItem.GraphicsItemFlag.ItemIsMovable|QGraphicsItem.GraphicsItemFlag.ItemIsSelectable)#可选择可移动
                item.setCacheMode(QGraphicsItem.CacheMode.ItemCoordinateCache)
                item.setRect(210,60,100,50)
                item.setStartAngle(45)
                self.scene.addItem(item)
            case 'Polygon':#多边形
                item =QGraphicsPolygonItem()
                item.setData(self.DataFlag.ItemType.value,itemType)
                item.setData(self.DataFlag.ItemName.value,f'{itemType}_{uuid.uuid1().hex}')
                item.setFlags(QGraphicsItem.GraphicsItemFlag.ItemIsMovable|QGraphicsItem.GraphicsItemFlag.ItemIsSelectable)#可选择可移动
                item.setCacheMode(QGraphicsItem.CacheMode.ItemCoordinateCache)
                item.setPolygon([QPointF(-55,-35),QPointF(55,35),QPointF(55,-35),QPointF(-55,35)])                
                self.scene.addItem(item)
            case 'Path':
                item =QGraphicsPathItem()
                item.setData(self.DataFlag.ItemType.value,itemType)
                item.setData(self.DataFlag.ItemName.value,f'{itemType}_{uuid.uuid1().hex}')
                item.setFlags(QGraphicsItem.GraphicsItemFlag.ItemIsMovable|QGraphicsItem.GraphicsItemFlag.ItemIsSelectable)#可选择可移动
                item.setCacheMode(QGraphicsItem.CacheMode.ItemCoordinateCache)
                path=QPainterPath()
                path.moveTo(0,0)
                path.cubicTo(120,-20,60,100,120,80)
                item.setPath(path)          
                self.scene.addItem(item)
            case 'Pixmap':#图片
                item =QGraphicsPixmapItem()                
                item.setData(self.DataFlag.ItemType.value,itemType)
                item.setData(self.DataFlag.ItemName.value,f'{itemType}_{uuid.uuid1().hex}')
                item.setFlags(QGraphicsItem.GraphicsItemFlag.ItemIsMovable|QGraphicsItem.GraphicsItemFlag.ItemIsSelectable)#可选择可移动
                item.setCacheMode(QGraphicsItem.CacheMode.ItemCoordinateCache)
                item.setPixmap(QPixmap(f'{self.parent.getAppDir()}/resources/qtforpython.png'))                             
                self.scene.addItem(item)
            case _:
                pass
        
        self.parent.OnShowMessage(f'Add Item {itemType}')
    def Remove(self):
        item= self.scene.selectedItems()[0]
        itemName=item.data(self.DataFlag.ItemName.value)
        self.parent.OnShowMessage(f'{itemName} Removed')
        self.scene.removeItem(item)
    def Group(self):
        if len(self.scene.selectedItems())==0:
            return
        group= self.scene.createItemGroup(self.scene.selectedItems())
        group.setFlags(QGraphicsItem.GraphicsItemFlag.ItemIsMovable|QGraphicsItem.GraphicsItemFlag.ItemIsSelectable)#可选择可移动      
        group.setData(self.DataFlag.ItemType.value,'Group')
        group.setData(self.DataFlag.ItemName.value,f'Group_{uuid.uuid1().hex}')
        self.parent.OnShowMessage(f'Add Item Group')
        
    def UnGroup(self):
        if len(self.scene.selectedItems())==0:return
        item = self.scene.selectedItems()[0]        
        self.parent.OnShowMessage(f'UnGroup {item.data(self.DataFlag.ItemName.value)}')
        self.scene.destroyItemGroup(item)
    def OnDragModeChanged(self,text):
        mode=QGraphicsView.DragMode[text]
        self.view.setDragMode(mode)
        self.parent.OnShowMessage(f'Set DragMode:{mode}')
    def OnCacheModeChanged(self,text):
        mode=QGraphicsView.CacheModeFlag[text]
        self.view.setCacheMode(mode)
        self.parent.OnShowMessage(f'Set CacheMode:{mode}')
    def OnOptimizationChanged(self,text):
        op=QGraphicsView.OptimizationFlag[text]
        self.view.setOptimizationFlag(op)
        self.parent.OnShowMessage(f'Set OptimizationFlag:{op}')
    def OnViewportUpdateModeChanged(self,text):
        mode=QGraphicsView.ViewportUpdateMode[text]
        self.view.setViewportUpdateMode(mode)
        self.parent.OnShowMessage(f'Set ViewportUpdateMode:{mode}')
    def OnItemSelectionChanged(self):
        if len(self.scene.selectedItems())==0:
            return
        item= self.scene.selectedItems()[0]
        itemName=item.data(self.DataFlag.ItemName.value)
        self.parent.OnShowMessage(f'{itemName} Selected')
    def OnMouseMove(self,event):
        if len(self.scene.selectedItems())==0:
            return
        item= self.scene.selectedItems()[0]
        itemName=item.data(self.DataFlag.ItemName.value)
        pos=event.pos()
        self.parent.OnShowMessage(f'{itemName} Moving, ({pos.x()},{pos.y()})')
def runDemo(parent):
    wigdet =  Demo(parent)
    return wigdet